package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
<#if restControllerStyle>
    import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @date ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
    class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    private String prefix = "<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>";

    @Autowired
    private ${table.serviceName} ${table.entityPath}Service;

    @RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}:</#if>${table.entityPath}:view")
    @GetMapping()
    public String ${table.entityPath}() {
        return prefix + "/${table.entityPath}";
    }

    @ApiOperation("查询${table.comment}列表")
    @RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}:</#if>${table.entityPath}:list")
    @PostMapping(value = "/list")
    @ResponseBody
    public TableDataInfo list(${entity} ${table.entityPath}) {
        QueryWrapper<${entity}> qw = new QueryWrapper(${table.entityPath});
        Page page = getPage(qw);
        IPage result = ${table.entityPath}Service.page(page, qw);
        return getDataTable(result);
    }

    @ApiOperation("导出${table.comment}列表")
    @RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}:</#if>${table.entityPath}:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(${entity} ${table.entityPath}) {
    List<${entity}> list = ${table.entityPath}Service.list(new QueryWrapper<>(${table.entityPath}));
        ExcelUtil<${entity}> util = new ExcelUtil<${entity}>(${entity}.class);
        return util.exportExcel(list, "${table.entityPath}");
    }

    @ApiOperation("新增${table.comment}")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @ApiOperation("新增保存${table.comment}")
    @RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}:</#if>${table.entityPath}:add")
    @Log(title = "${table.comment}", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(${entity} ${table.entityPath}) {
        return toAjax(${table.entityPath}Service.save(${table.entityPath}));
    }

    @ApiOperation("修改${table.comment}")
    @GetMapping("/edit/{${table.entityPath}Id}")
    public String edit(@PathVariable("${table.entityPath}Id") String ${table.entityPath}Id, ModelMap mmap) {
        ${entity} ${table.entityPath} = ${table.entityPath}Service.getById(${table.entityPath}Id);
        mmap.put("${table.entityPath}", ${table.entityPath});
        return prefix + "/edit";
    }

    @ApiOperation("修改保存${table.comment}")
    @RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}:</#if>${table.entityPath}:edit")
    @Log(title = "${table.comment}", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(${entity} ${table.entityPath}) {
        return toAjax(${table.entityPath}Service.updateById(${table.entityPath}));
    }

    @ApiOperation("删除${table.comment}")
    @RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}:</#if>${table.entityPath}:remove")
    @Log(title = "${table.comment}", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(${table.entityPath}Service.removeByIds(Arrays.asList(ids.split(","))));
    }
}
</#if>
